<?php

/**
 * contact : 刘经理 19326416371
 * company : 版权所属 四川北星惯牌科技有限公司
 **/

namespace app\api\controller;

use app\api\validate\CaptainTeam;
use app\common\controller\Api;
use app\common\manager\CaptainManager;
use think\Config;
use think\Exception;

/**
 * 队长中心
 */
class Captain extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }

    /**
     * 队长个人信息及中心信息
     */
    public function index()
    {
        try {

        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    /**
     * 队长入驻
     * @param $name 姓名
     * @param $idcard 身份证
     * @param $introduce 简介
     * @return void
     */
    public function application(){
        $user_id = $this->user_id;
        try {
            if ($this->user->is_authentication != \app\common\model\User::IS_AUTHENTICATION_YES){
                throw new Exception('您还未实名认证，无法申请入驻队长，请前往个人中心实名认证');
            }
            if ($this->user->is_captain){
                throw new Exception('您已入驻队长，请勿重复操作');
            }

            $result = CaptainManager::AuthRealName($user_id);
            if ($result['code'] == 1) {
                $this->success($result['message']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }

    }

    /**
     * 获取入驻信息
     * @return void
     */
    public function applyInfo()
    {
        $userId = $this->user_id;
        try {
            $result = CaptainManager::getCaptain($userId);
            if ($result['code'] == 1) {
                $this->success($result['message'],$result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    public function writeOffRecord()
    {
        $page = $this->request->post('page') ?? 1;
        $userId = $this->user_id;
        try {
            $result = CaptainManager::getWriteOffRecord($userId,$page);
            if ($result['code'] == 1) {
                $this->success($result['message'],$result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }
}
